import qs from "qs";
import axios from "axios";
// import baseUrl from "./config";
import '../util/checkStatus'
let localStorage = window.localStorage;
let baseURL = process.env.VUE_APP_BASE_API
// axios.defaults.transformRequest = [
//   function (data) {
//     if (data instanceof FormData) {
//       return data;
//     }
//     return qs.stringify(data, { arrayFormat: "repeat" });
//   },
// ];
/*
 * 增加请求拦截器
 */
var loading;
axios.interceptors.request.use(
  (config) => {
    return config;
  },
  (error) => {
    // 预处理请求有异常error时抛出错误
    return Promise.reject(error);
  }
);

/*
 * 增加相应拦截器
 */
axios.interceptors.response.use(
  (response) => {
    return response;
  },
  (error) => {
    // return Promise.reject(checkStatus(error));
  }
);
/**
 * 状态码校验
 * @param response
 * @return {*}
 */
function checkStatus(response) {
  // 有响应时校验状态
  if (response) {
    // -10 自己定义，连接错误的status
    const status = response.status || -10;
    if (status === 200 || status === 304 || status === 400) {
      return response.data;
    } else {
      let errorInfo = "";
      switch (status) {
        case -1:
          errorInfo = "远程服务响应失败,请稍后重试";
          break;
        case 400:
          errorInfo = "400: 错误请求";
          break;
        case 401:
          errorInfo = "401: 访问令牌无效或已过期";
          break;
        case 403:
          errorInfo = "403: 拒绝访问";
          break;
        case 404:
          errorInfo = "404：资源不存在";
          break;
        case 405:
          errorInfo = "405: 请求方法未允许";
          break;
        case 408:
          errorInfo = "408: 请求超时";
          break;
        case 500:
          errorInfo = "500：访问服务失败";
          break;
        case 501:
          errorInfo = "501：未实现";
          break;
        case 502:
          errorInfo = "502：无效网关";
          break;
        case 503:
          errorInfo = "503: 服务不可用";
          break;
        // 其他错误
        default:
          errorInfo = `连接错误${status}`;
      }
      return {
        status,
        msg: errorInfo,
      };
    }
  }
  // 异常状态下，返回错误信息
  return {
    status: -404,
    msg: "网络异常",
  };
}


/**
 * 返回axios方法
 * @param url（如果传绝对地址则baseURL不会追加到url之前）
 * @param method
 * @param timeout
 * @param data
 * @param params
 * @param headers
 * @param dataType
 * @returns {AxiosPromise}
 * 
 */
export default function (
  url,
  {
    // 默认求情方式post
    method = "post",
    // 超时
    timeout = 100000,
    // timeout = 58310,
    // 请求主题
    data = {},
    // 请求头
    headers = {
      "Content-Type": "application/json;charset=UTF-8;",
      // "Content-Type": "application/x-www-form-urlencoded",
      // sessionId: localStorage.sessionId,
      // token:'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsYXN0TG9naW5UaW1lIjoiMTY1NjIyMzkxOTAwMCIsInVwZGF0ZWRUaW1lIjoiMTY1NjIxMDEwNTAwMCIsImlzcyI6Im9wZXJhdG9yX3VzZXIiLCJuYW1lIjoi5bCP5a6LIiwiY3JlYXRlZFRpbWUiOiIxNjQ2NzkzNzE1MDAwIiwiZXhwIjoxNjU3NTI3NzcyLCJ1c2VySWQiOiI1MyIsImxhc3RMb2dpbklwIjoiMTkyLjE2OC4xLjEwNyIsInN0YXR1cyI6IjEiLCJ1c2VybmFtZSI6InNvbmd0ZW5nZmVpIn0.Uh8l7AVhkhLr_LKeTiuCJaZZiojc5tL3at12L4WP3xc'
      // token: localStorage.token ? localStorage.token : '',
    },
    // headers = {
    //   'User-Agent': 'Apifox/1.0.0 (https://apifox.com)',
    //   'Content-Type': 'application/json'
    // },
    // 文件类型
    dataType = "json",
    responseType = "",
  }
) {
  const config = {
    method: method,
    timeout: timeout,
    url: url,
    // 在外部文件配置axios的基础路径 ip地址
    baseURL: baseURL,
    data: data,
    // headers: headers,
    dataType: dataType,
    responseType: responseType,
  };
  return axios(config);
}
